layui.use(['form', 'layer', 'laydate'], function () {

    var $ = layui.$
        , form = layui.form
        , laydate = layui.laydate;

     function setLayVerify(id, isRequired) {
         let checkDiv = $('#'+id).get(0);
         if (isRequired) {
             checkDiv.setAttribute('lay-verify', 'required');
         } else {
             checkDiv.setAttribute('lay-verify', '');
         }
    }

    let cxHbchecked = false;
    let cxMhchecked = false;
    var gzShow = false;
    var qnShow = false;
    var tcShow = false;
    form.on('checkbox(platformName)',function (data){

        if (data.value == 1 || data.value == 2) {
            let set_price = 'set_price';
            let cabin_price = 'cabin_price';
            if (data.value == 1) {
                cxHbchecked = data.elem.checked;
            } else  {
                cxMhchecked = data.elem.checked;
            }
            if (cxHbchecked || cxMhchecked) {
                $('#xc-item').show();
                setLayVerify(set_price, true);
                setLayVerify(cabin_price, true);
            } else if (!cxHbchecked && !cxMhchecked) {
                $('#xc-item').hide();
                setLayVerify(set_price, false);
                setLayVerify(cabin_price, false);
            }
        } else if (data.value == 3) {
            let compare_price = 'compare_price';
            let one_dec_price = 'one_dec_price';
            if (!gzShow) {
                $('#gz-item').show();
                setLayVerify(compare_price, true);
                setLayVerify(one_dec_price, true);
                gzShow = true;
            } else {
                $('#gz-item').hide();
                setLayVerify(compare_price, false);
                setLayVerify(one_dec_price, false);
                gzShow = false;
            }
        } else if (data.value == 4) {
            let seller_price = 'seller_price';
            let auto_price_price = 'auto_price_price';
            if (!qnShow) {
                $('#qn-item').show();
                setLayVerify(seller_price, true);
                setLayVerify(auto_price_price, true);
                checkProductType();
                qnShow = true;
            } else {
                $('#qn-item').hide();
                setLayVerify(seller_price, false);
                setLayVerify(auto_price_price, false);
                checkProductType();
                qnShow = false;
            }
        } else if (data.value == 5) {
            let fix_price = 'fix_price';
            let auto_price_price = 'fix_price_biding';
            if (!tcShow) {
                $('#tc-item').show();
                setLayVerify(fix_price, true);
                setLayVerify(auto_price_price, true);
                tcShow = true;
            } else {
                $('#tc-item').hide();
                setLayVerify(fix_price, false);
                setLayVerify(auto_price_price, false);
                tcShow = false;
            }
        }


        // 校验是否选择了上传平台
        let platformDiv = 'platformDiv';
        let platformCheckbox = $('#'+platformDiv).find('input[type="checkbox"]');
        let checkedCount = platformCheckbox.filter(':checked').length;
        if (checkedCount == 0) {
            setLayVerify(platformDiv, true);
        } else {
            setLayVerify(platformDiv, false);
        }
    });

    // 监听航司的编号，展示产品类型
    //输入框的值改变时触发
    $("#AirLine").on("input", function (e) {
        checkProductType();
    });

    // 去哪产品类型
    var isMarkTypeShow = false;
    function checkProductType() {
        let divEle = $('#HaveAirline');
        let newDivEle = divEle.get(0);
        let checked = $('input[type="checkbox"][name="qn"]').get(0).checked;
        if (checked) {
            $('#qn-productMarkDiv').show();
            let airLine = $('#AirLine').val();
            if (airLine != null && airLine != '') {
                if (!isMarkTypeShow) {
                    $('#noAirline').hide();
                    $('#HaveAirline').show();
                    isMarkTypeShow = true;
                }
            } else {
                $('#noAirline').show();
                $('#HaveAirline').hide();
                isMarkTypeShow = false;
            }

            let checkbox = divEle.find('input[type="checkbox"][name="productMark[]"]');
            if (isMarkTypeShow) {
                let checkedCount = checkbox.filter(':checked').length;
                if (checkedCount == 0) {
                    newDivEle.setAttribute("lay-verify", "required");
                } else {
                    newDivEle.setAttribute("lay-verify", "");
                }
            } else {
                newDivEle.setAttribute("lay-verify", "");
            }
        } else {
            $('#qn-productMarkDiv').hide();
            newDivEle.setAttribute("lay-verify", "");
        }
    }

   //监听产品类型
    form.on('checkbox(productMark)', function (data) {
        checkProductType();
    });

    // 航司有默认值时，产品类型也显示
    if ($('#AirLine').val().length > 0) {
        checkProductType();
    }

    //销售日期
    laydate.render({
        elem: '#sale-date'
        , type: 'date'
        , range: ['#sale-start', '#sale-end']
        , rangeLinked: true // 是否开启日期范围选择时的区间联动标注模式
        //,value: ['2022-05-01', '2022-06-01']
    });

    //监听库存类型
    form.on('radio(InventoryType)', function (data) {
        checkSeat();
    });

    function checkSeat() {
        let priceRuleValue = $('input[type="radio"][name="InventoryType"]:checked').val();
        let checkDiv = $('#seat').get(0);
        if (priceRuleValue == 'FIX') {
            checkDiv.setAttribute('lay-verify', 'required|number');
        } else {
            checkDiv.setAttribute('lay-verify', '');
        }
    }

    checkSeat();

})